// components/HalfScreen/index.js
import { request } from '../../utils/util'
const { $Message } = require('../../dist/base/index');
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    fileList: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    fileList: [], //半屏弹窗的列表
    fileListLength: 1,//选中的数量
    checkTitle: "全选", //全选的文本
    openScrollTop: 0 //初始滚动
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showHalfScreen() {
      const appData = app.globalData.halfScreen
      const value = appData.id || appData.key
    
      if (this.properties.fileList.length === 0 || appData.isOrgRoot) {
        return
      } else {
        const fileList = this.properties.fileList
        fileList.forEach(item => {
          item.value = item.key || item.id
          if (item.value === value && item.types.toLowerCase() === appData.type) {
            item.checked = true
          } else {
            item.checked = false
          }

        })
        
        let openScrollTop
        fileList.map((item, index) => {
          if (item.checked) {
            if (index > 2) {
              openScrollTop = (index - 2) * 61
            }
          }
        })
       
        this.setData({
          ...this.data,
          fileList: fileList,
          openScrollTop: openScrollTop,
          show: true
        })
      }

    },
    /**
 * 半屏弹窗的关闭
 */
    halfClose() {
      this.setData({
        ...this.data,
        show: false,
        fileListLength: 1,
        openScrollTop: 0,
      })
    },
    /**
     * 单选按钮的点击事件
     * @param {*} e 
     */
    radioChange(e) {
      const tapVal = e.currentTarget.dataset.value
      const newList = this.data.fileList;
      let listLength = 0
      newList.forEach(el => {
        if (el.value === tapVal) {
          el.checked = !el.checked
        }
        if (el.checked) {
          listLength = listLength + 1
        }
      })
      this.setData({
        ...this.data,
        fileList: newList,
        fileListLength: listLength,
        openScrollTop: undefined
      })

    },

    /**
     * 全选
     */
    checkAll() {
      const title = this.data.checkTitle
      const newList = this.data.fileList
      if (title === "全选") {
        newList.forEach(el => {
          el.checked = true
        })
        this.setData({
          ...this.data,
          fileList: newList,
          fileListLength: newList.length,
          checkTitle: "取消"
        })
      } else {
        newList.forEach(el => {
          newList.forEach(el => {
            el.checked = false
          })

          this.setData({
            ...this.data,
            fileList: newList,
            fileListLength: 0,
            checkTitle: "全选"
          })
        })
      }
    },
    /**
     * 批量删除
     */
    handleClick() {
      const that = this
  
      const fileList = this.data.fileList
      let key = []
      let id = []
      fileList.forEach(el => {
        if (el.checked) {
          if (el.key) {
            key.push(el.key)
          }
          if (el.id) {
            id.push(el.id)
          }
        }
      })
      if(key.length === 0 && id.length === 0){
        wx.showToast({
          title: '未选中要删除的内容',
          icon:"none"
        })
        return
      }
      const paramKey = {
        deleteid: key
      }
      const paramId = {
        deleteid: id
      }
      if (key.length > 0 && id.length === 0) {
        request("DELETE", "/api/service/catalog/multiple_delete/", paramKey, "", true, "删除中")
          .then(res => {
            if (res.statusCode === 427) {
              this.setData({
                ...this.data,
                show: false
              })
            }
            if (res.statusCode === 200) {
              that.triggerEvent("getFileList")
              that.setData({
                ...that.data,
                show: false
              })
              $Message({
                content: '删除成功！',
                type: 'success'
              });
            }
          })

      } else if (id.length > 0 && key.length === 0) {
        request("DELETE", "/api/service/docinfo/multiple_delete/", paramId, "", true, "删除中")
          .then(res => {
            if (res.statusCode === 427) {
              this.setData({
                ...this.data,
                show: false
              })
            }
            if (res.statusCode === 200) {
              that.triggerEvent("getFileList")
              that.setData({
                ...that.data,
                show: false
              })
              $Message({
                content: '删除成功！',
                type: 'success'
              });
            }
          })
      } else {
        request("DELETE", "/api/service/catalog/multiple_delete/", paramKey, "", true, "删除中")
          .then(res => {
            if (res.statusCode === 427) {
              this.setData({
                ...this.data,
                show: false
              })
            }
            if (res.statusCode === 200) {
              request("DELETE", "/api/service/docinfo/multiple_delete/", paramId, "", true, "删除中")
                .then(res => {
                  if (res.statusCode === 200) {
                    that.triggerEvent("getFileList")
                    that.setData({
                      ...that.data,
                      show: false
                    })
                    $Message({
                      content: '删除成功！',
                      type: 'success'
                    });
                  }
                })
            }
          })
      }

    }
  }
})
